using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class SerializeEnumParser
    {
        public static Dictionary<string, int> Parse(string enumFilePath, out int maxValue)
        {
            maxValue = 0;
            if (!File.Exists(enumFilePath))
            {
                Console.WriteLine($"File {enumFilePath} not found.");
                return null;
            }

            string fileContent = File.ReadAllText(enumFilePath);

            // 正则表达式匹配命名空间和枚举内容
            // 匹配每个枚举项
            string enumItemPattern = @"(\w+)\s*=\s*(\d+)";
            MatchCollection matches = Regex.Matches(fileContent, enumItemPattern);
            Dictionary<string, int> enumItems = new();

            foreach (Match enumMatch in matches)
            {
                string name = enumMatch.Groups[1].Value;
                if (name == "None")
                {
                    continue;
                }
                int value = int.Parse(enumMatch.Groups[2].Value);

                enumItems.Add(name, value);
                maxValue = Math.Max(value, maxValue);
            }

            return enumItems;
        }
    }
}
